﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;
using System.Globalization;
using System.Threading;

public partial class Registry : System.Web.UI.Page
{
    ACCOUNTSDataAPI api = new ACCOUNTSDataAPI();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] == null)
                Response.Redirect("AdminLogin.aspx");
            lblsSecurityCode.Text = Common.GenerateRodomCode(9, 19);
            btnUpdate.Visible = false;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        DateTime dToDate = ConvertToDate(txtToDate.Text);
        DateTime dFromDate = ConvertToDate(txtFromDate.Text);
        string password=txtPassword.Text.Trim();
        string email=txtEmail.Text.Trim();        
        bool payment = false;
        string securityCode = lblsSecurityCode.Text.Trim();
        if (rdlAutoPayment.SelectedValue == "1")
            payment = true;
        if (CheckEmail(txtEmail.Text.Trim()))
        {
            lblMessages.Text = "Email is exists! Do you want to use this email?";
            btnUpdate.Visible = true;
            return;
        }
        int checkDate = dFromDate.CompareTo(dToDate);
        if (checkDate == 0 || checkDate == 1)
        {
            lblMessages.Text = "ToDate greater than FromDate!";
            return;
        }
        bool chek = api.insertACCOUNTS(txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), securityCode, dFromDate, dToDate, false, payment);
        if (chek)
        {
            lblMessages.Text = "Register successful!";
            sendMail(email,password,securityCode);
            sendMailOne(email, password, securityCode);
        }
        else
            lblMessages.Text = "Register Fail!";

    }
    public bool CheckEmail(string email)
    {
        DataTable dt = api.getAccountByEmail(email);
        if (dt != null)
            return true;
        return false;
    }
    public DateTime ConvertToDate(string date)
    {
        string result = string.Empty;
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        int day = DateTime.Now.Day;
        if (date.Trim() != null && date.Trim() != string.Empty)
        {
            string[] strarr = date.Split('/');
            year = int.Parse(strarr[2].ToString());
            month = int.Parse(strarr[0].ToString());
            day = int.Parse(strarr[1].ToString());
        }
        CultureInfo en = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentCulture = en;

        // Creates a DateTime for the local time.
        DateTime dt = new DateTime(year, month, day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

        return dt;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        DateTime dToDate = ConvertToDate(txtToDate.Text);
        DateTime dFromDate = ConvertToDate(txtFromDate.Text);
        string password = txtPassword.Text.Trim();
        string email = txtEmail.Text.Trim();        
        string securityCode = lblsSecurityCode.Text;
        bool payment = false;
        if (rdlAutoPayment.SelectedValue == "1")
            payment = true;
        int checkDate = dFromDate.CompareTo(dToDate);
        if (checkDate == 0 || checkDate == 1)
        {
            lblMessages.Text = "ToDate greater than FromDate!";
            return;
        }
        int accountId = 0;
        DataTable dt = api.getAccountByEmail(txtEmail.Text.Trim());
        if (dt != null)
            accountId = int.Parse(dt.Rows[0]["ACC_ID"].ToString());
        if (api.updateACCOUNTS(accountId, txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), securityCode, dFromDate, dToDate, false, payment))
        {
            lblMessages.Text = "Update successful!";
            sendMail(email,password,securityCode);
            sendMailOne(email,password,securityCode);
        }
    }

    public void sendMailOne(string email, string password, string securityCode)
    {
        try
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.From = new System.Net.Mail.MailAddress("admin@appsfactoryltd.com");
            msg.To.Add(new System.Net.Mail.MailAddress(email));

            msg.Subject = "Congratulation your registry account successful";
            string body = "<html><body><div>Congratulation your registry account successful ";
            body += "from <a href='http://appsfactoryltd.com'>appsfactoryltd.com</a><br/><br/>";
            body += " EmailLogin: " + email;
            body += " <br/><br/>";
            body += "Password: " + password;
            body += " <br/><br/> SecurityCode: " + securityCode;
            body += "</div><br/><br/></body></html>";

            msg.Body = body;
            msg.IsBodyHtml = true;

            System.Net.Mail.SmtpPermission x = new System.Net.Mail.SmtpPermission(System.Net.Mail.SmtpAccess.ConnectToUnrestrictedPort);

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.EnableSsl = false;
            smtp.Host = "mail.appsfactoryltd.com";
            smtp.Port = 1025;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("admin", "admin", "appsfactoryltd.com");
            smtp.Send(msg);
        }
        catch
        {

        }

    }
    public void sendMail(string email, string password, string securityCode)
    {
        try
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.From = new System.Net.Mail.MailAddress("admin@appsfactoryltd.com");
            msg.To.Add(new System.Net.Mail.MailAddress(email));
            msg.Subject = "Congratulation your registry account successful";
            string body = "<html><body><div>Congratulation your registry account successful ";
            body += "from <a href='http://appsfactoryltd.com'>appsfactoryltd.com</a><br/><br/>";
            body += " UserName: " + email;
            body += " <br/><br/>";
            body += "Password: " + password;
            body += " <br/><br/> SecurityCode: " + securityCode;
            body += "</div><br/><br/></body></html>";
            msg.Body = body;
            msg.IsBodyHtml = true;

            System.Net.Mail.SmtpPermission x = new System.Net.Mail.SmtpPermission(System.Net.Mail.SmtpAccess.ConnectToUnrestrictedPort);

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

            smtp.EnableSsl = false;
            smtp.Host = "mail.appsfactoryltd.com";
            smtp.Port = 25;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("admin", "admin", "appsfactoryltd.com");
            smtp.Send(msg);
        }
        catch 
        {

            
        }

    }

}
